<template>
  <h1>El-员工列表综合练习</h1>
  <el-form label-width="auto" style="width: 40%;margin: 0 auto;">
    <el-form-item label="姓名">
      <el-input  v-model="staff.name"  placeholder="请输入员工姓名" ></el-input>
    </el-form-item>
    <el-form-item label="工资">
      <el-input  v-model="staff.salary" placeholder="请输入员工工资"></el-input>
    </el-form-item>
    <el-form-item label="岗位">
      <el-input v-model="staff.job" @keyup.enter="add" placeholder="请输入员工岗位"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add">提交</el-button>
    </el-form-item>
  </el-form>
  <el-table :data="staffs" style="width: 40%;margin: 0 auto;">
    <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
    <el-table-column label="姓名" prop="name"></el-table-column>
    <el-table-column label="工资" prop="salary"></el-table-column>
    <el-table-column label="岗位" prop="job"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

const staff = ref(
    {
      name:'',
      salary:'',
      job:''
    });
const staffs = ref([
    {name:'der圣',salary:'10000',job:'准研究生'}
]);
const add = () =>{
  if(staff.value.name.trim()=='' || staff.value.salary.trim()=='' || staff.value.job.trim()==''){
    //alert('请输入员工的完整信息!');
    ElMessage.error('请输入员工的完整信息!');
    return;
  }
  staffs.value.push(staff.value);
  staff.value = {
    name:'',
    salary:'',
    job:''
  }
}
const del = (index,stf) =>{
  console.log(index,stf);
  if(confirm('是否要删除'+stf.name+'员工？')){
    staffs.value.splice(index,1);
    ElMessage.success(stf.name+'员工已删除')
  }

}

</script>

<style scoped>

</style>